package com.panda.pandawash.adapter;

/**
 * Created by gaowf on 2017/2/24.
 */

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.panda.pandawash.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class LeDeviceListAdapter extends BaseAdapter implements OnClickListener {
    private ArrayList<BluetoothDevice> mLeDevices;
    public ArrayList<Map<String, Object>> updateDevicesList;
    public ArrayList<Map<String, Object>> devicesList;

    private ArrayList<Map<String, Object>> bindlist = new ArrayList<Map<String, Object>>();
    private LayoutInflater mInflator;
    private Context mContext;
    private Map<String, Object> map;
    private MyCallback mCallback;
    private String updateName;
    private EditText editName;

    public interface MyCallback {
        public void click(View v);
    }

    public LeDeviceListAdapter(Context mContext, MyCallback mCallback, ArrayList<Map<String, Object>> devicesList, ArrayList<Map<String, Object>> bindList) {

        mLeDevices = new ArrayList<BluetoothDevice>();
        updateDevicesList = new ArrayList<Map<String, Object>>();
        if (devicesList != null) {
            updateDevicesList = devicesList;
        }
        for (Map<String, Object> map : updateDevicesList) {
            if (map.get("devices_name") != null && map.get("devices_name").toString().length() > 0) {
                Log.d("bear_", map.get("devices_name").toString());
            } else {
            }
        }

        this.bindlist = bindList;
        mInflator = LayoutInflater.from(mContext);
        this.mContext = mContext;
        this.mCallback = mCallback;
    }

    public void addDevice(Map<String, Object> map) {

        SharedPreferences myPreferences = mContext.getSharedPreferences("btDevices", mContext.MODE_PRIVATE);

        int i = myPreferences.getInt("count", 0);
        String addDeviceAddress = map.get("devices_address").toString();
        String addDevicName = map.get("devices_name").toString();



        if (i > 0) {
            for (int j = 1; j <= i; j++) {
                String addrString = myPreferences.getString("address", "");
                String nameString = myPreferences.getString("name", "");

                if (addrString.equals(addDeviceAddress)  ) {
                    Log.i("bear__", "addDeviceAddress: " + addDeviceAddress);
                    map = new HashMap<String, Object>();
                    map.put("devices_name", nameString);
                    map.put("devices_address", addrString);
                }

            }
        }

        Log.i("bear_", "map = " + map.get("devices_name"));
        updateDevicesList.add(map);
    }

    public void updateDeviceName(int position, String name) {

        boolean flg = true;

        devicesList = new ArrayList<Map<String, Object>>();
        devicesList = updateDevicesList;
        String addressString = devicesList.get(position).get("devices_address").toString();
        //Log.i("bear_", devicesList.get(position) + "______________0");
        map = new HashMap<String, Object>();
        map.put("devices_name", name);
        map.put("devices_address", addressString);
        devicesList.set(position, map);
        //Log.i("bear_", devicesList.get(position) + "______________1");

        SharedPreferences myPreferences = mContext.getSharedPreferences("btDevices", mContext.MODE_PRIVATE);
        SharedPreferences.Editor editor = myPreferences.edit();
        int i = myPreferences.getInt("count", 0);

        //Log.i("bear_", "count ============ " + i );

        if (i > 0) {
            for (int j = 1; j <= i; j++) {
                String addrString = myPreferences.getString("address", "");
                if (addrString.equals(addressString)) {
                    editor.putString("name", name);
                    editor.commit();
                    flg = false;
                }
            }
        }

        if (flg) {
            editor.putInt("count", i += 1);
            editor.putString("name", name);
            editor.putString("address", addressString);
            editor.commit();
        }

    }

    public Map<String, Object> getDevice(int position) {
        return updateDevicesList.get(position);
    }

    public void clear() {
        updateDevicesList.clear();
    }

    @Override
    public int getCount() {
        return updateDevicesList.size();
    }

    @Override
    public Object getItem(int i) {
        return updateDevicesList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        // General ListView optimization code.
        if (view == null) {
            view = mInflator.inflate(R.layout.listitem_device, null);
            viewHolder = new ViewHolder();
            viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
            viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
            viewHolder.deviceStatus = (TextView) view.findViewById(R.id.device_status);
            // viewHolder.editName =
            // (EditText)findViewById(R.id.edit_device_name);
            viewHolder.btnUpdate = (Button) view.findViewById(R.id.btn_modify_name);
            editName = (EditText) view.findViewById(R.id.edit_device_name);

            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        // BluetoothDevice device = mLeDevices.get(i);
        String deviceName = updateDevicesList.get(i).get("devices_name").toString();
        boolean deviceStatus = (Boolean) updateDevicesList.get(i).get("device_bind_status");
        if (deviceName != null && deviceName.length() > 0)
            viewHolder.deviceName.setText(deviceName);
        else
            viewHolder.deviceName.setText("未知的设备");

        viewHolder.deviceAddress.setText(updateDevicesList.get(i).get("devices_address").toString());

        if (deviceStatus) {
            viewHolder.deviceStatus.setText("已绑定");
        } else {
            viewHolder.deviceStatus.setText("");
        }

        viewHolder.btnUpdate.setOnClickListener(this);
        viewHolder.btnUpdate.setTag(i);

        return view;
    }

    public class ViewHolder {
        TextView deviceName;
        TextView deviceAddress;
        TextView deviceStatus;
        Button btnUpdate;

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        mCallback.click(v);
    }

}
